package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author luojia
 * @create 2022-01-01 23:11
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    /**
     * 分页查询供应商
     *
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @Override
    public String querySupplier(Integer page, Integer rows, String supplierName) {
        Integer initialRow = (page - 1) * rows;
        List<Supplier> supplierList = supplierDao.querySupplier(initialRow, rows, supplierName);

        int size = supplierList.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", size);
        map.put("rows", supplierList);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    /**
     * 供应商添加或修改
     *
     * @param supplier
     * @param supplierId
     * @return
     */
    @Override
    public void supplierAddOrUpdate(Supplier supplier, Integer supplierId) {
        if (supplierId == null) {
            // 添加
            supplierDao.addSupplier(supplier);
        } else {
            // 更新
            supplierDao.updateSupplier(supplier, supplierId);
        }
    }

    /**
     * 删除供应商（支持批量删除）
     *
     * @param split
     * @return
     */
    @Override
    public void supplierDelete(String[] split) {
        supplierDao.supplierDelete(split);
    }
}
